//Demo
layui.use('form', function(){
    var form = layui.form;

    //监听提交
    form.on('submit(formDemo)', function(data){
        layer.msg(JSON.stringify(data.field));
        return false;
    });
});
////测试
//表单
layui.use(['table'], function(){
    var table = layui.table;
    var tree = layui.tree;
    table.render({
        elem: '#user'
        ,size: 'lg' //开启大尺寸样式
        ,url:'/admin/user/page'
        ,cols: [[
            {type:'checkbox'}
            // {field: 'id', title: 'id', sort: true, fixed: 'left'}
            ,{field: 'username', title: '用户名',width:100}
            ,{field: 'nickname', title: '昵称',width:100}
            ,{field: 'roleName', title: '角色',width:100}
            ,{field: 'createTime', title: '创建时间'}
            ,{title: "操作", fixed: 'right', align: 'center', toolbar: '#bar'}
        ]]
        ,page: true
        ,id: 'select'
    });
    //模糊查询
    $("#select").click(function () {
        var username = $("#selectun").val()
        var nickname = $("#selectnn").val()
        var role = $("#selectrole").val();

        console.log(role)
        var formData = {
            username:username
            ,nickname:nickname
            ,roleId: role
            ,page:1
            ,limit:10
        }
        $.ajax({
            url:"/admin/user/page"
            ,method:"get"
            ,data:formData
            ,success: (response) => {
                console.log(response)
                table.reload('select',{
                    where: {
                        username:username
                        ,nickname:nickname
                        ,roleId: role

                    }
                    ,page:{
                        curr:1
                    }
                })
            },
        })

    })
    table.on('tool(user)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
        var id = data.stringId;
        if (layEvent == "select") {//查看
            $.ajax({
                url:'/admin/user/get/' + id
                ,method: 'get'
                ,success: (response) => {
                    console.log(response)
                    $("#uname").val(response.data.username);
                    $("#nname").val(response.data.nickname);
                    $("#rname").val(response.data.roleName)
                    $("#ctime").val(response.data.createTime);
                    var user_open = layer.open({
                        type: 1,
                        area: ['400px', '400px'],
                        title: "权限列表"
                        ,content: $("#sel_user")
                        ,shade: 0
                        ,btn: []
                        ,cancel: function (layero, index) {//关闭按钮
                            layer.close(user_open);
                        },
                    })
                }
            });
        }else if(layEvent == "update"){
            $.ajax({
                url:'/admin/user/get/' + id
                ,method: 'get'
                ,success: (response) => {
                    console.log(response)
                    $("#up_uname").val(response.data.username);
                    $("#up_nname").val(response.data.nickname);
                    var user_open = layer.open({
                        type: 1,
                        area: ['400px', '260px'],
                        title: "权限列表"
                        ,content: $("#update_user")
                        ,shade: 0
                        ,btn: ["修改","取消"]
                        ,btn1:function(index,layero){
                            var username = $("#up_uname").val()
                            var nickname = $("#up_nname").val()
                            var id = response.data.stringId
                            var formData = {
                                username:username
                                ,nickname:nickname
                                ,id:id
                            }
                            $.ajax({
                                url:"/admin/user/update/"
                                ,method:"put"
                                ,data:formData
                                ,success:(response) => {
                                    console.log(response)
                                    if (response.code == 0) {
                                        alert("修改角色成功")
                                        location.reload()
                                    } else {
                                        alert(response.msg)
                                        return false
                                    }
                                }
                            })
                        }
                        ,cancel: function (layero, index) {//关闭按钮
                            layer.close(user_open);

                        },
                    })
                }
            });
        }else if(layEvent == "update2"){//更改权限
            var role = layer.open({
                type:1
                ,area:['350px','200px']
                ,title: "设置角色"
                ,content: $("#upd_user")
                ,shade: 0
                ,btn: ['提交', '重置']
                ,btn1: function(index, layero){//提交按钮
                    var role_id = $("#update_user").val()
                    var super_user_id = id
                    console.log(super_user_id)
                    console.log(role_id)
                    $.ajax({
                        url: '/admin/user/set_role',
                        method: "put",
                        data: {roleId: role_id, superUserId: super_user_id},
                        success: (response) => {
                            if (response.code == 0) {
                                alert("分配角色成功")
                                location.reload()
                            } else {
                                alert(response.msg)
                                return false
                            }
                        },
                    })
                },
                btn2: function(index, layero){//重置按钮
                    return false
                },
                cancel: function(layero,index){//关闭按钮
                    layer.close(role);
                }
            });
        }else if (layEvent == "delete"){
            var ids = [];
            ids.push(id)
            layer.confirm('你确认要删除此角色么', {
                time: 0,
                title: '删除角色',
                btn: ['确认', '取消'],
            }, function (index) {
                $.ajax({
                    url:"/admin/user/delete"
                    ,method : "delete"
                    ,data:{'superUserIdArray':ids}
                    ,success: (response)=>{
                        if (response.code == 0){
                            alert("删除成功")
                            location.reload()
                        }else{
                            alert(response.msg)
                        }
                    }
                });
            },function (index) {
                layer.close(index);
            });
        }
    });
});


//添加用户
$(function () {
    $("#add").click(function () {
        var add_tags = layer.open({
            type:1,
            area:['350px','350px'],
            title: "新增标签"
            ,content: $("#add_user"),
            shade: 0,
            btn: ['提交', '重置']
            ,btn1: function(index, layero){//提交按钮
                var username = $("#username").val()
                var password = $("#password").val()
                var nickname = $("#nickname").val()

                if(username == null || username == ""){
                    alert("用户名不能为空")
                    return false;
                }if(password == null || password == ""){
                    alert("密码不能为空")
                    return false;
                }if(nickname == null || nickname == ""){
                    alert("昵称不能为空")
                    return false;
                }

                var formData = {
                    username: username,
                    password: password,
                    nickname: nickname
                }
                $.ajax({
                    url: '/admin/user/add',
                    method: "post",
                    data: formData,
                    success: (response) => {
                        if (response.code == 0) {
                            alert("创建用户成功")
                            location.reload()
                        } else {
                            alert(response.msg)
                            return false
                        }
                    },
                })
            },
            btn2: function(index, layero){//重置按钮
                $("#username").val("")
                $("#password").val("")
                $("#nickname").val("")
                return false
            },
            cancel: function(layero,index){//关闭按钮
                layer.close(add_tags);
            }
        });
    })

})
//批量删除用户
$(function(){
    $("#dels").click(function () {
        var ids = [];
        var id_data = layui.table.checkStatus('select').data
        for (var i = 0; i < id_data.length; i+=1){
            ids.push(id_data[i]['stringId']);
        }
        if (id_data.length == 0){
            alert("请选择要删除的信息")
        }else{
            layer.confirm('你确认要删除这些角色么', {
                time: 0,
                title: '批量角色',
                btn: ['确认', '取消'],
            }, function (index) {
                $.ajax({
                    url:"/admin/user/delete"
                    ,method : "delete"
                    ,data:{'superUserIdArray':ids}
                    ,success: (response)=>{
                        if (response.code == 0){
                            alert("删除成功")
                            location.reload();
                        }else{
                            alert(response.msg)
                        }
                    }
                });
            },function (index) {
                layer.close(index);
            });
        }
    })
})

